package km

import (
	"database/sql"

	"log"

	"strings"

	_ "github.com/alexbrainman/odbc"
	"github.com/axgle/mahonia"
)

type BiTItemInfo struct {
	ItemNo        string
	ItemSubNo     string
	ItemSubName   string
	ItemClsno     string
	UnitNo        string
	SalePrice     float64
	LasterInprice float64
	LastDate      float64
}

func GetFreshInfo() [2][]*BiTItemInfo {
	var is [2][]*BiTItemInfo

	conn, err := sql.Open("odbc", KMJXCConnStr)
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	stmt1, err := conn.Prepare(`SELECT top 5 [detail].[item_no],[info].[item_subno],[info].[unit_no],[price].[sale_price],[info].[item_subname],[info].[item_clsno] FROM bi_t_item_info as [info],ic_t_inout_store_master as [master],ic_t_inout_store_detail as [detail],bi_t_item_price as [price] WHERE ([master].[sheet_no] = [detail].[sheet_no]) and ([detail].[item_no] = [info].[item_no]) and [price].[branch_no] = [master].[branch_no] and [price].[item_no] = [info].[item_no] and ( [master].[approve_flag] = '1' ) and ( datediff(day,[master].[oper_date],getdate())) < ? and ( [info].[item_clsno] like ? ) and [master].[branch_no] = '03' order by [master].[oper_date] desc,[info].[item_clsno]`)
	if err != nil {
		panic(err)
	}
	defer stmt1.Close()

	dec := mahonia.NewDecoder("gbk")
	//0301 蔬菜 0302 水果
	cls := []string{"0301", "0302"}
	//select the xinxing
	for i := 1; len(is[1]) == 0 && i < 3; i++ {

		for _, v := range cls {
			//day clsno branch_no
			row, err := stmt1.Query(i, v+"%")
			if err != nil {
				log.Println(err)
			}
			defer row.Close()
			for row.Next() {
				var info BiTItemInfo
				row.Scan(&info.ItemNo, &info.ItemSubNo, &info.UnitNo, &info.SalePrice, &info.ItemSubName, &info.ItemClsno)
				info.ItemSubName = dec.ConvertString(info.ItemSubName)
				info.UnitNo = dec.ConvertString(info.UnitNo)
				info.ItemNo = strings.TrimSpace(info.ItemNo)
				info.ItemClsno = strings.TrimSpace(info.ItemClsno)
				is[1] = append(is[1], &info)
			}
		}
	}
	//select the shengshidongfang
	stmt0, err := conn.Prepare(`SELECT top 5 [detail].[item_no],[info].[item_subno],[info].[unit_no],[info].[sale_price],[info].[item_subname],[info].[item_clsno] FROM bi_t_item_info as [info],ic_t_inout_store_master as [master],ic_t_inout_store_detail as [detail] WHERE ([master].[sheet_no] = [detail].[sheet_no]) and ([detail].[item_no] = [info].[item_no]) and ( [master].[approve_flag] = '1' ) and ( datediff(day,[master].[oper_date],getdate())) < ? and ( [info].[item_clsno] like ? ) and [master].[branch_no] = '00' order by [master].[oper_date] desc,[info].[item_clsno]`)
	if err != nil {
		panic(err)
	}
	defer stmt0.Close()
	for i := 1; len(is[0]) == 0 && i < 3; i++ {
		for _, v := range cls {
			//day clsno branch_no
			row, err := stmt0.Query(i, v+"%")
			if err != nil {
				log.Println(err)
			}
			defer row.Close()
			for row.Next() {
				var info BiTItemInfo
				row.Scan(&info.ItemNo, &info.ItemSubNo, &info.UnitNo, &info.SalePrice, &info.ItemSubName, &info.ItemClsno)
				info.ItemSubName = dec.ConvertString(info.ItemSubName)
				info.ItemNo = strings.TrimSpace(info.ItemNo)
				info.ItemClsno = strings.TrimSpace(info.ItemClsno)
				info.UnitNo = dec.ConvertString(info.UnitNo)
				is[0] = append(is[0], &info)
			}
		}
	}

	return is

}
